class fake_db_connect():
    '''
    模拟数据库连接工具
    '''
    import json
    def __init__(self,filename:str):
        self.filename=filename
        with open(filename, 'r',encoding='utf-8') as f:
            self.fake_user_table = self.json.load(f)
    
    def get(self,primaryKey:str)->dict:
        return self.fake_user_table.get(primaryKey)

    def insert(self,info:dict):
        primaryKey=info['username']
        self.fake_user_table[primaryKey]=info

    def commit(self):
        with open(self.filename, 'w',encoding='utf-8') as f:
            self.json.dump(self.fake_user_table, f, ensure_ascii=False,indent=2)
    
    def delete(self,primaryKey:str):
        del self.fake_user_table[primaryKey]


if __name__=="__main__":
    #模拟连接数据库
    table=fake_db_connect('fake_data.json')

    #模拟查数据
    user01=table.get('johndoe')
    print(user01)

    #模拟新增数据
    newuser={
        "username": "lihua",
        "full_name": "Li Hua",
        "email": "lihua@example.com",
        "hashed_password": "fakehashedmima",
        "disabled": True,
    }
    table.insert(newuser)
    table.commit()#提交更改



    username=input("要查询的人名：")#输入"lihua"，可以查询到信息
    user02=table.get(username)
    print(user02)